package com.demo.third;

import com.github.binarywang.demo.wx.cp.aes.HttpUt;
import com.github.binarywang.demo.wx.cp.controller.ThirdBaseController;
import sun.awt.geom.AreaOp;

/**
 * created by lanxinghua@2dfire.com on 2020/2/15
 * 企业第三方应用：推送消息
 */
public class ThirdMessageDemo extends ThirdBaseController {
    private static final String MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
    public static void main(String[] args) throws Exception{
        String accessToken = getAccessToken(true);
        Integer chooseIndex = 4;
        switch (chooseIndex){
            case 0:{
                text(accessToken);
                break;
            }
            case 1:{
                image(accessToken);
                break;
            }
            case 2:{
                voice(accessToken);
                break;
            }
            case 3:{
                video(accessToken);
                break;
            }
            case 4:{
                textcard(accessToken);
                break;
            }
            case 5:{
                news(accessToken);
                break;
            }
            default:{
                break;
            }
        }
    }

    /**
     * 文本消息
     * @param accessToken
     * @throws Exception
     */
    private static void text(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"touser\" : \"ChenXingXing\",\n" +
            "   \"msgtype\" : \"text\",\n" +
            "   \"agentid\" : 1000014,\n" +
            "   \"text\" : {\n" +
            "       \"content\" : \"你的快递已到，请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>，聪明避开排队。\"\n" +
            "   },\n" +
            "   \"safe\":0,\n" +
            "   \"enable_id_trans\": 0,\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }


    /**
     * 图片消息
     * @param accessToken
     * @throws Exception
     */
    private static void image(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"touser\" : \"lanxinghua@2dfire.com\",\n" +
            "   \"msgtype\" : \"image\",\n" +
            "   \"agentid\" : 1000014,\n" +
            "   \"image\" : {\n" +
            "        \"media_id\" : \"1B_iNntVxelxURNgQd_xTto1VdKpSHJ98ZkLw3iqfGbQ5t3TVivXXv_4gLbkPhDmZ\"\n" +
            "   },\n" +
            "   \"safe\":0,\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }


    /**
     * 语音消息
     * @param accessToken
     * @throws Exception
     */
    private static void voice(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"touser\" : \"ChenXingXing\",\n" +
            "   \"msgtype\" : \"voice\",\n" +
            "   \"agentid\" : 1000014,\n" +
            "   \"voice\" : {\n" +
            "        \"media_id\" : \"1a1n4WmxMbTjF_DRj6RFV7s0_fUZPIIo_TThU0j64Gt8\"\n" +
            "   },\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }


    /**
     * 视频消息
     * @param accessToken
     * @throws Exception
     */
    private static void video(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"touser\" : \"ChenXingXing\",\n" +
            "   \"msgtype\" : \"video\",\n" +
            "   \"agentid\" : 1000014,\n" +
            "   \"video\" : {\n" +
            "        \"media_id\" : \"1Q3dHqp3o1kz868ZlbbV1oJUUOKYRY7Gbf-jjqoZF1Ved4TtCmi0fvrfew5OaWOBu\",\n" +
            "        \"title\" : \"视频title\",\n" +
            "       \"description\" : \"第三方服务商下发视频测试\"\n" +
            "   },\n" +
            "   \"safe\":0,\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }


    /**
     * 文本卡片消息
     * @param accessToken
     * @throws Exception
     */
    private static void textcard(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"toparty\" : \"86714333\",\n" +
            "   \"msgtype\" : \"textcard\",\n" +
            "   \"agentid\" : 1000016,\n" +
            "   \"textcard\" : {\n" +
            "            \"title\" : \"领奖通知 测试用\",\n" +
            "            \"description\" : \"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系lanxinghua</div>\",\n" +
            "            \"url\" : \"http://2dfire.com\",\n" +
            "            \"btntxt\":\"更多\"\n" +
            "   },\n" +
            "   \"enable_id_trans\": 0,\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }

    /**
     * 图文消息
     * @param accessToken
     * @throws Exception
     */
    private static void news(String accessToken) throws Exception {
        String post = "{\n" +
            "   \"toparty\" : \"86714333\",\n" +
            "   \"msgtype\" : \"news\",\n" +
            "   \"agentid\" : 1000016,\n" +
            "   \"news\" : {\n" +
            "       \"articles\" : [\n" +
            "           {\n" +
            "               \"title\" : \"二维火智慧商圈中秋节礼品领取\",\n" +
            "               \"description\" : \"今年中秋节公司有豪礼相送\",\n" +
            "               \"url\" : \"http://2dfire.com\",\n" +
            "               \"picurl\" : \"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png\"\n" +
            "           }\n" +
            "        ]\n" +
            "   },\n" +
            "   \"enable_id_trans\": 0,\n" +
            "   \"enable_duplicate_check\": 0\n" +
            "}\n";
        com.alibaba.fastjson.JSONObject result = HttpUt.sendPost(String.format(MSG_URL, accessToken), post);
        System.out.println("发送文本消息："+result.toJSONString());
    }

}
